using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using System.IO;
using System;
using System.Threading.Tasks;
using System.Collections.Generic;

namespace B.S.BasicData.Api.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class UploadMultiController : ControllerBase
    {
        [HttpPost("images")]
        public async Task<IActionResult> UploadImages([FromForm] List<IFormFile> file)
        {
            if (file == null || file.Count == 0)
                return BadRequest("未选择文件");

            var uploadsFolder = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "uploads");
            if (!Directory.Exists(uploadsFolder))
                Directory.CreateDirectory(uploadsFolder);

            var paths = new List<string>();
            foreach (var files in file)
            {
                if (files.Length > 0)
                {
                    var fileExt = Path.GetExtension(files.FileName);
                    var fileName = $"{Guid.NewGuid()}{fileExt}";
                    var filePath = Path.Combine(uploadsFolder, fileName);
                    using (var stream = new FileStream(filePath, FileMode.Create))
                    {
                        await files.CopyToAsync(stream);
                    }
                    paths.Add($"/uploads/{fileName}");
                }
            }
            return Ok(new { paths });
        }
    }
} 